﻿/*
*功能描述：服务端分页服务  js调用封装
*/

//#region 客户端分页
var ServerPager = Class(Object, {
    //构造函数 （实例字符串,需要分页的数据总集合，分页页宽（每页显示数据））
    //这些属性在构造函数中赋值了， 但是可以在后续引用代码中 通过对象.属性的方式重新给属性赋值
    Create: function (pageBindInfo) {
        this.PageBindInfo = pageBindInfo;
        this.QueryData = {};
        this.ajaxSer = new AjaxRequestTool();
        this.SuccessCallBack = {};
        this.Method = "";
        this.CoverId = "";

        //#region 注册分页按钮时间
        var _this = this;
        $("#btnNextPage").unbind().click(function () {
            _this.GoNextPage();

        });

        $("#btnPrePage").unbind().click(function () {
            _this.GoPrePage();

        });

        $("#btnFirstPage").unbind().click(function () {
            _this.GoFistPage();

        });

        $("#btnLastPage").unbind().click(function () {
            _this.GoLastPage();

        });

        //#endregion
    },

    /*初始化数据，包括：
     method：数据服务地址
     queryData:参数参数
     successCallBack：成功回调函数（公共分页所做的事情已经在本类中全部实现了，successCallBack是做公共分页意外的事）
     coverId:遮罩层ID
    */
    InitServerPager: function (method, queryData, successCallBack,coverId) {
        var _this = this;
        _this.QueryData = queryData;
        _this.SuccessCallBack = successCallBack;
        _this.Method = method;
        _this.CoverId = coverId;
    },

    /*调用服务获取数据*/
    GetServerPagerData: function() {
        var _this = this;


        _this.PageBindInfo.PagerInfo([]);
        _this.PageBindInfo.PagerTxt("当前查询共0条数据，每页显示10条，分1显示 当前第<span style='font-weight:800'>1</span>页");
        _this.MaxPagerIndex = 1;

        _this.ajaxSer.RequestByPost(_this.Method,  _this.QueryData , function(data) {

            var queryRst = new PageEntity();

            queryRst.PageIndex = data.PageIndex;
            queryRst.PageSize = data.PageSize;
            queryRst.PageCount = data.PageCount;
            queryRst.RowCount = data.RowCount;
            queryRst.OrderColumnName = _this.QueryData.OrderColumnName;
            queryRst.IsOrderByDesc = _this.QueryData.IsOrderByDesc;

            queryRst.PageData = data.PageData;
            _this.PageBindInfo.PagerInfo(queryRst);
            _this.PageBindInfo.PagerTxt("当前查询共{0}条数据，每页显示{1}条，分{2}显示 当前第<span style='font-weight:800'>{3}</span>页".Format(data.RowCount, data.PageSize, data.PageCount, data.PageIndex));
            _this.MaxPagerIndex = data.PageCount;
            _this.SuccessCallBack();


        }, function() {},_this.CoverId);
    },

    ///刷新当前查询条件数据
    FreshData: function() {
        this.GetServerPagerData();
    },

    //#region 翻页跳转
   
    /*上一页*/
    GoPrePage: function() {
        var _this = this;
        if (_this.QueryData.PageIndex > 1) {
            _this.QueryData.PageIndex--;
        }
        _this.GetServerPagerData();
    },

    /*下一页*/
    GoNextPage: function() {
        var _this = this;
        if (_this.QueryData.PageIndex < _this.PageBindInfo.PagerInfo().PageCount) {
            _this.QueryData.PageIndex++;
            _this.GetServerPagerData();
        }
       
    },
    
    /*首页*/
    GoFistPage: function() {
        var _this = this;
        if (_this.QueryData.PageIndex > 1) {
            _this.QueryData.PageIndex = 1;
            _this.GetServerPagerData();
        }
    },

    /*尾页*/
    GoLastPage: function() {
        var _this = this;
        if (_this.QueryData.PageIndex < _this.PageBindInfo.PagerInfo().PageCount) {
            _this.QueryData.PageIndex = _this.PageBindInfo.PagerInfo().PageCount;
            _this.GetServerPagerData();
        }
       
    },


    /*跳转到指定页*/
    GoPage: function (n) {
        var _this = this; 
        if (n > 1 && n < _this.PageBindInfo.PagerInfo().PageCount() && n !== _this.QueryData.PageIndex()) {
            _this.QueryData.PageIndex = n;
            _this.GetServerPagerData();
        }
    }


//#endregion 
});